//
//  HSDrawerController.swift
//  Home_School_08
//
//  Created by 张晓林 on 2016/12/23.
//  Copyright © 2016年 张晓林. All rights reserved.
//

import UIKit

class HSDrawerController: UIViewController {
    
    // 左边视图控制器
    let leftVc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HSLeftViewController")
    // 标签视图控制器
    let tabbarVc = HSTabBarController()
    // 动画时长
    let animationTime: TimeInterval = 0.5
    // 设置左边视图的宽度
    let leftViewW: CGFloat = screenW * 0.842
    // 左边视图的最大偏移量
    let leftViewMaxOffset: CGFloat = screenW / 2
    // 记录手指开始位置
    var startP: CGPoint = CGPoint()
    
    // 计算偏移距离的比例
    var scaleOffset: CGFloat {
            return leftViewMaxOffset / (leftViewW + 5)
    }
    
    // 懒加载遮盖视图
    lazy var coverView: UIButton = {
        let btn = UIButton(type: UIButtonType.custom)
        btn.frame = UIScreen.main.bounds
        btn.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1)
        // 设置透明
        btn.alpha = 0.0
        return btn
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 
        view.backgroundColor = #colorLiteral(red: 0.9999960065, green: 1, blue: 1, alpha: 1)
    
        setupUI()
        
        // 添加拖动手势
        let panGesture = UIPanGestureRecognizer(target: self, action: #selector(panGestureChange(gesture:)))
        view.addGestureRecognizer(panGesture)
    }

    // 设置UI效果
    func setupUI() {
        // 添加左边控制器及视图
        addChildViewController(leftVc)
        // 添加控制器作为自控制时，必须同时添加视图作为子视图
        view.addSubview(leftVc.view)
        // 设置左边视图默认大小
        leftVc.view.frame = CGRect(x: 0, y: 0, width: leftViewW, height: screenH)
        // 设置右边阴影
        leftVc.view.layer.shadowColor = UIColor.black.cgColor
        leftVc.view.layer.shadowOffset = CGSize(width: 3, height: 0)
        leftVc.view.layer.shadowOpacity = 1.0
        // 设置默认的形变属性
        leftVc.view.transform = CGAffineTransform(translationX: -leftViewMaxOffset, y: 0)
  
        // 添加标签控制器作为右边控制器
        addChildViewController(tabbarVc)
        view.addSubview(tabbarVc.view)
        // 赋值闭包代码块
        tabbarVc.leftBtnClock = {[weak self] in
            if (self?.tabbarVc.view.transform.tx)! > 0 {
                self?.closeAnimation()
            }else {
                self?.openAnimation()
            }
        }
    }
    
    // 拖动手势的回调方法
    func panGestureChange(gesture:UIPanGestureRecognizer) {
        switch gesture.state {
        case .began:
            // 获得当前第一次的触摸点
            startP = gesture.location(in: gesture.view)
        case .changed:
            // 获得当前手指位移量
            let offset = gesture.translation(in: gesture.view)
            
            // 计算两个视图的偏移量
            let newOffset = tabbarVc.view.transform.tx + offset.x
            
            // 判断当偏移大于最大偏移量时的处理
            if newOffset >= (leftViewW + 5) {
                openAnimation()
                return
            }
            
            // 当左边越界时的处理
            if newOffset <= 0 {
                closeAnimation()
                return
            }
            // 计算左边视图的偏移量
            let newOffset_leftV = leftVc.view.transform.tx + offset.x * scaleOffset
            // 标签视图的偏移
            tabbarVc.view.transform = CGAffineTransform(translationX: newOffset, y: 0)
            // 左边视图的偏移
            leftVc.view.transform = CGAffineTransform(translationX: newOffset_leftV, y: 0)
            // 恢复手势移动的偏移量
            gesture.setTranslation(CGPoint(), in: gesture.view)
            
        case .ended:
            let endP = gesture.location(in: gesture.view)
            if endP.x > startP.x {
                openAnimation()
            }else {
                closeAnimation()
            }
            
        default:
            break
        }
    }
    
    // 打开抽屉的方法
    func openAnimation() {
        // 添加遮盖视图
        tabbarVc.view.addSubview(coverView)
        coverView.addTarget(self, action: #selector(closeAnimation), for: .touchUpInside)
        // 移动标签控制器视图
        UIView.animate(withDuration: animationTime) {[weak self] in
            self?.tabbarVc.view.transform = CGAffineTransform(translationX: (self?.leftViewW)! + 5, y: 0)
            // 设置遮盖的透明度
            self?.coverView.alpha = 0.6
            // 设置左边视图的动画偏移
            self?.leftVc.view.transform = CGAffineTransform.identity
        }
    }
    
    // 关闭抽屉效果
    func closeAnimation() {
        
        UIView.animate(withDuration: animationTime, animations: {
            self.tabbarVc.view.transform = CGAffineTransform.identity
            // 设置遮盖透明度
            self.coverView.alpha = 0.0
            
            self.leftVc.view.transform = CGAffineTransform(translationX: -self.leftViewMaxOffset, y: 0)
        }) { (_) in
            // 移除当前遮盖视图
            self.coverView.removeFromSuperview()
        }
    }
    
}









